package com.oracle.oa.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.BaseBiz;
import com.oracle.oa.biz.FileinfoBiz;
import com.oracle.oa.biz.impl.OperatelogBizImpl;
import com.oracle.oa.entity.Accessoryfile;
import com.oracle.oa.entity.Departinfo;
import com.oracle.oa.entity.Fileinfo;
import com.oracle.oa.entity.Userinfo;

public class FileinfoAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;
	Map<String, Object> session = null;
	private Fileinfo fileinfo;
	private String filename;
	private String createname;
	private Date begintime;
	private Date endtime;
	private String whiletime;
	private String url;
	private long fileid;
	private long parentid;
	private String parentFilename;
	private String type;
	private int where;
	private int result;
	private List<Fileinfo> findFileList = new ArrayList<Fileinfo>();
	BaseBiz bBiz;
	FileinfoBiz fBiz;
	// 上传文件
	/* 上传文件存放的目录 */
	private final static String UPLOAD_URL = "/upload";
	/* 上传文件的集合 */
	private List<File> file; // 必须与表单元素的NAME属性值一致
	/* 上传文件名的集合 */
	private List<String> fileFileName;
	// 必须跟随文件名再追加FileName
	/* 上传文件类型的集合 */
	private List<String> fileFileContentType;

	// 必须跟随文件名再追加fileFileContentType

	public String getOtherDepart(){
		String hql="from Departinfo d where d.departid !="+((Userinfo)(session.get("userinfo"))).getDepartid();
		List<Departinfo> dList=fBiz.getAllByHQL(hql);
		session.put("otherDepart", dList);
		return "getOtherDepart";
	}
	public String geturl(){
		if(session.get("url")!=null){
			url=session.get("url").toString();
		}else{
			url="";
		}
		return url;
	}
	/**
	 * 上传文件
	 * 
	 * @return
	 */
	public String uploadFile() {
		System.out.println("...........................");
		for (int i = 0; i < file.size(); i++) {
			try {
				this.upload(i); // 自定义方法调用
			} catch (Exception e) {

				e.printStackTrace();
			}

		}

		url = "/"+geturl()+"/fileManage/upload.jsp";
        result=1;
		return "uploadFile";
	}

	private void upload(int i) throws Exception {
		InputStream in = new FileInputStream(file.get(i));

		String dir = ServletActionContext.getServletContext().getRealPath(
				UPLOAD_URL);

	
		File uploadFile = new File(dir, this.getFileFileName().get(i)); // 上传的文件
		OutputStream out = new FileOutputStream(uploadFile);

		byte[] buffer = new byte[1024 * 1024];

		int length;
		while ((length = in.read(buffer)) > 0) {

			out.write(buffer, 0, length);
			Accessoryfile a=new Accessoryfile();
			a.setAccessoryname(uploadFile.getName());
			a.setAccessorypath("/upload/"+uploadFile.getName());
			a.setAccessorysize(0);  //uploadFile.length()
            a.setCreatedate(new Timestamp(new Date().getTime()));
            a.setIfdelete(0);
            System.out.println(a.getAccessorypath());
            session.put("tempAcc", a);
		}
		if (in != null) 
			in.close();


		if (out != null)
			out.close();

	}

	/**
	 * 新建除文件夹以外的文件
	 * 
	 * @return
	 */
	public String add() {
		fileinfo.setUserinfo((Userinfo)session.get("userinfo"));
		fileinfo.setIfdelete(0);
		fileinfo.setCreatedate(new Timestamp(new Date().getTime()));
		fileinfo.setFilepath(fileinfo.getFilepath() + "\"" + parentFilename);
		fBiz.save(fileinfo);
		String hql = "select max(f.fileid) FROM Fileinfo f";
		long l=(Long) fBiz.getAllByHQL(hql).get(0);
		Fileinfo f=(Fileinfo) fBiz.getById(Fileinfo.class, l);
        Accessoryfile a=(Accessoryfile) session.get("tempAcc");
        a.setFileinfo(f);
        a.setFiletypeinfo(f.getFiletypeinfo());
        fBiz.save(a);
        fileid = f.getParentid();
		session.put("fileid", fileid);
		url = "tree_findChild";
		OperatelogBizImpl.add("上传文件", "上传文件"+"\""+a.getAccessoryname()+"\"");
		return "add";
	}

	/**
	 * 新建文件夹
	 * 
	 * @return
	 */
	public String folderAdd() {
		fileinfo.setIfdelete(0);
		fileinfo.setFilepath(fileinfo.getFilepath() + "\"" + parentFilename);
		System.out.println("filepath=" + fileinfo.getFilepath());
		fileinfo.setCreatedate(new Timestamp(new Date().getTime()));
		fBiz.save(fileinfo);
		fileid = fileinfo.getParentid();
		session.put("fileid", fileid);
		url = "tree_findChild";
		OperatelogBizImpl.add("新建文件夹", "新建文件夹"+"\""+fileinfo.getFilename()+"\"");
		return "folderAdd";
	}

	/**
	 * 传递文件路径
	 * 
	 * @return
	 */
	public String getFilepath() {
		fileinfo = (Fileinfo) fBiz.getById(Fileinfo.class, fileid);
		if (type.equals("1")) {
			url = "../"+geturl()+"/fileManage/folderAdd.jsp";
		} else {
			url = "../"+geturl()+"/fileManage/fileAdd.jsp";
		}
		return "getFilepath";
	}

	/**
	 * 从回收站中还原文件
	 * 
	 * @return
	 */
	public String back() {
		System.out.println("********back*******" + fileid);
		fBiz.toRecycleBin(fileid, 0l);
		fileinfo=(Fileinfo) fBiz.getById(Fileinfo.class, fileid);
		OperatelogBizImpl.add("撤回删除", "从回收箱中撤回对文件"+"\""+fileinfo.getFilename()+"\""+"的删除");
		return "back";
	}

	/**
	 * 从回收站中永久删除文件
	 * 
	 * @return
	 */
	public String deleteForever() {
		System.out.println("*******deleteForever********" + fileid);
		Fileinfo f = (Fileinfo) fBiz.getById(Fileinfo.class, fileid);
		toDelete(f);
		OperatelogBizImpl.add("删除", "从回收站中永久删除文件" +"\""+ f.getFilename()+"\"");
		return "deleteForever";
	}

	public void toDelete(Fileinfo f) {
		if (f.getFiletypeinfo().getFiletypeid() == 1l) {
			List<Fileinfo> list = fBiz.getByProperty(Fileinfo.class,
					"parentid", f.getFileid());
			if (list.size() > 0) {
				for (Fileinfo ff : list) {
					toDelete(ff);
				}
				bBiz.delete(f);
			} else {
				bBiz.delete(f);
			}
		} else {
			Accessoryfile a = (Accessoryfile) bBiz.getByProperty(
					Accessoryfile.class, "fileinfo.fileid", fileid).get(0);

			bBiz.delete(a);
			bBiz.delete(f);
		}
	}

	/**
	 * 修改文件信息
	 * 
	 * @return
	 */
	public String update() {
		 fileid=fileinfo.getFileid();
		 System.out.println(fileid+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		if(session.get("tempAcc")!=null){
			Accessoryfile acc = (Accessoryfile) bBiz.getByProperty(
					Accessoryfile.class, "fileinfo.fileid", fileid).get(0);
			fBiz.delete(acc);
			Accessoryfile a=new Accessoryfile();
			a=(Accessoryfile) session.get("tempAcc");
			a.setFileinfo(fileinfo);
	        a.setFiletypeinfo(fileinfo.getFiletypeinfo());
	        fBiz.save(a);
		}
		fileid = fileinfo.getParentid();
		fBiz.update(fileinfo);
		session.put("fileid", fileid);
		url = "tree_findChild";
		OperatelogBizImpl.add("修改", "修改"+"\""+fileinfo.getFilename()+"\""+"文件的信息");
		return "update";
	}

	/**
	 * 文件查询
	 * 
	 * @return
	 */
	public String findFile() {
		findFileList = fBiz.findFile(filename, createname, begintime, endtime,
				whiletime);

		session.put("findFileList", findFileList);
		session.put("where", 2);
		url = "../"+geturl()+"/fileManage/showFileFind.jsp";
		return "find";
	}

	/**
	 * 将文件删除，即改变文件是否删除状态为1l（放入回收站）
	 * 
	 * @return
	 */
	public String toRecycleBin() {

		fBiz.toRecycleBin(fileid, 1l);
		fileinfo = (Fileinfo) fBiz.getById(Fileinfo.class, fileid);
		// url = "/admin_cg/fileManage/recycleBin.jsp";

		fileid = fileinfo.getParentid();
		session.put("fileid", fileid);
		url = "tree_findChild";
		OperatelogBizImpl.add("放入回收站", "将文件"+"\""+fileinfo.getFilename()+"\""+"放入回收站");
		return "toRecycleBin";
	}

	/**
	 * 查询回收站中所有文件
	 * 
	 * @return
	 */
	public String recyleBin() {
		List<Fileinfo> list = new ArrayList<Fileinfo>();
		list = fBiz.getByProperty(Fileinfo.class, "ifdelete", 1l);
		session.put("recyleBinList", list);
		url = "../"+geturl()+"/fileManage/recycleBin.jsp";
		return "recyleBin";
	}

	public Fileinfo getFileinfo() {
		return fileinfo;
	}

	public void setFileinfo(Fileinfo fileinfo) {
		this.fileinfo = fileinfo;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getCreatename() {
		return createname;
	}

	public void setCreatename(String createname) {
		this.createname = createname;
	}

	public Date getBegintime() {
		return begintime;
	}

	public void setBegintime(Date begintime) {

		this.begintime = begintime;

	}

	public Date getEndtime() {
		return endtime;
	}

	public void setEndtime(Date endtime) {

		this.endtime = endtime;

	}

	public String getWhiletime() {
		return whiletime;
	}

	public void setWhiletime(String whiletime) {
		this.whiletime = whiletime;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<Fileinfo> getFindFileList() {
		return findFileList;
	}

	public void setFindFileList(List<Fileinfo> findFileList) {
		this.findFileList = findFileList;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public BaseBiz getbBiz() {
		return bBiz;
	}

	public void setbBiz(BaseBiz bBiz) {
		this.bBiz = bBiz;
	}

	public FileinfoBiz getfBiz() {
		return fBiz;
	}

	public void setfBiz(FileinfoBiz fBiz) {
		this.fBiz = fBiz;
	}

	public long getFileid() {
		return fileid;
	}

	public void setFileid(long fileid) {
		this.fileid = fileid;
	}

	public long getParentid() {
		return parentid;
	}

	public void setParentid(long parentid) {
		this.parentid = parentid;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileFileContentType() {
		return fileFileContentType;
	}

	public void setFileFileContentType(List<String> fileFileContentType) {
		this.fileFileContentType = fileFileContentType;
	}

	public static String getUploadUrl() {
		return UPLOAD_URL;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getParentFilename() {
		return parentFilename;
	}

	public void setParentFilename(String parentFilename) {
		this.parentFilename = parentFilename;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getWhere() {
		return where;
	}

	public void setWhere(int where) {
		this.where = where;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

}
